package org.grow.bill.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.grow.bill.dao.CommodityBatchDao;
import org.grow.bill.dao.PurchaseBillDao;
import org.grow.bill.entity.CommodityBatch;
import org.grow.bill.entity.PurchaseBill;
import org.grow.bill.entity.enums.PurchaseBillState;
import org.grow.bill.service.PurchaseBillService;
import org.grow.bill.utils.CodeGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @Author: xwg
 * @CreateDate: 21-8-11
 */

@RestController
@RequestMapping("/bill/purchase")
public class PurchaseBillController {
    @Autowired
    private CommodityBatchDao commodityBatchDao;
    @Autowired
    private PurchaseBillDao purchaseBillDao;
    @Autowired
    private PurchaseBillService purchaseBillService;
    @Autowired
    private ObjectMapper objectMapper;
    @Resource
    private ThreadLocal<CodeGenerator> codeGeneratorThreadLocal;

    @PostMapping("/insert")
    public String insert(@RequestBody PurchaseBill purchaseBill) {
        try {
            System.out.println(objectMapper.writeValueAsString(purchaseBill));
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
//       1 purchaseBill 数据校验     //       1 batch 数据校验


//       2  purchaseBill 数据填充 后端填充

        purchaseBill.setPurchaseBillId(-1);
        purchaseBill.setIsDeleted(false);
        purchaseBill.setVersion(0);
//        从redis中辅助获取编码
        purchaseBill.setPurchaseBillCode(
                codeGeneratorThreadLocal.get().nextCode("PB"));
        LocalDateTime now = LocalDateTime.now();
        purchaseBill.setCreateTime(now);
        purchaseBill.setUpdateTime(now);
        purchaseBill.setDeliverTime(null);
        purchaseBill.setDeliver(null);
        purchaseBill.setDeliverPhone(null);
//        设置采购计划的发起人 和 验货人
        purchaseBill.setPlanner(SecurityContextHolder.getContext().getAuthentication().getName());
        purchaseBill.setExaminer(null);
//        核心状态字段
        purchaseBill.setPurchaseBillState(PurchaseBillState.notReceive);
//        计算预计总费用
        BigDecimal advancedCharge = new BigDecimal("0.00");
//       2  batch 数据填充 后端填充
        List<CommodityBatch> commodityBatches = purchaseBill.getCommodityBatches();
        for (CommodityBatch commodityBatch : commodityBatches) {
            commodityBatch.setIsDeleted(false);
            commodityBatch.setBatchId(-1);
            commodityBatch.setBatchCode(
                    codeGeneratorThreadLocal.get().nextCode("BC"));
            commodityBatch.setVersion(0);
            commodityBatch.setPurchaseBillFk(purchaseBill.getPurchaseBillCode());
//            计算费用
            BigDecimal r = commodityBatch.getPurchasePrice()
                    .multiply(
                            new BigDecimal(commodityBatch.getAdvanceQuantity()));
            advancedCharge= advancedCharge.add(r);

        }

        purchaseBill.setAdvanceCharge(advancedCharge);
        //3 spring tx事务管理 的保存 @transactional
        purchaseBillService.txInsert(purchaseBill);

        return purchaseBill.getPurchaseBillCode();

    }
}
